﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Windows.Devices.Geolocation;
using sdkMapControlWP8CS;

namespace VTMessenger
{
    public partial class MapPage : PhoneApplicationPage
    {
        const int MIN_ZOOM_LEVEL = 2;
        const int MAX_ZOOM_LEVEL = 20;
        private System.Device.Location.GeoCoordinate currentLocation;
        private bool isProcessingLocation;
        public MapPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void barLocation_Click(object sender, EventArgs e)
        {
            if (!isProcessingLocation)
                GetLocation(true);
            else
                MessageBox.Show("Processing current location. Please wait...");
        }

        private void barZoomIn_Click(object sender, EventArgs e)
        {
            if (map.ZoomLevel == MAX_ZOOM_LEVEL) return;
            map.ZoomLevel++;
        }

        private void barZoomOut_Click(object sender, EventArgs e)
        {
            if (map.ZoomLevel == MIN_ZOOM_LEVEL) return;
            map.ZoomLevel--;
        }

        private async void GetLocation(bool centerMap=false)
        {
            // Get current location.
            isProcessingLocation = true;
            Geolocator myGeolocator = new Geolocator();
            Geoposition myGeoposition = await myGeolocator.GetGeopositionAsync();
            Geocoordinate myGeocoordinate = myGeoposition.Coordinate;
            currentLocation = CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);
            isProcessingLocation = false;
            if (centerMap)
            {
                map.Center = currentLocation;
                map.ZoomLevel = 18;
            }
        }
    }
}